package crmdna.group;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import crmdna.client.Client;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.group.Group.GroupProp;

@SuppressWarnings("serial")
public class GroupServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html");

		Gson gson = new Gson();

		try {
			String client = req.getParameter("client");

			Client.ensureValidClient(client);

			List<GroupProp> groupProps = Group.getAll(client);

			APIResponse apiResponse = new APIResponse(Status.SUCCESS,
					groupProps);

			resp.getWriter().println(gson.toJson(apiResponse));

		} catch (Exception ex) {
			APIResponse apiResponse = new APIResponse(ex);
			resp.getWriter().println(gson.toJson(apiResponse));
		}
	}
}
